home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / TransSkel 3.24 / Demos / Pascal Demos / Hello / Hello.p next >
Text File  |  1996-01-25  |  5KB  |  200 lines

  1. { TransSkel "hello, world" application in Pascal }
  2.  
  3. { Presents a window with "Hello, world." displayed centered in the window }
  4. { when the window is active, and "Goodbye, world." when the window is }
  5. { inactive (e.g., when the application is suspended, or when the "About" }
  6. { alert comes up). }
  7.  
  8. { 04 Feb 94 Version 1.00, Paul DuBois }
  9. {
  10.  22 Jun 95
  11.  - Updated for TransSkel 3.22.  The phrase that is initially displayed
  12.  is set according to whether the application is launched into the
  13.  foreground or background.  In order for this to work properly, have to
  14.  install a handler for suspend/resume events to force an activate/deactivate
  15.  of the window (if launched into the background, no activate event happens
  16.  on the first resume, for some reason).
  17. }
  18.  
  19. program Hello;
  20.  
  21.     uses
  22.         Types, QuickDrawText, QuickDraw, Windows, Menus, ToolUtils, TransSkel;
  23.  
  24.     const
  25.  
  26. { strings that appear in window }
  27.  
  28.         hello = 'Hello, world.';
  29.         goodbye = 'Goodbye, world.';
  30.  
  31. { resource numbers }
  32.  
  33.         aboutAlrtRes = 1000;                { About box }
  34.         fileMenuNum = skelAppleMenuID + 1;    { File menu }
  35.  
  36. { File menu item numbers }
  37.  
  38.         quit = 1;
  39.  
  40.     var
  41.  
  42.         fileMenu: MenuHandle;
  43.  
  44.         phrase: Str255;                { current phrase }
  45.  
  46.  
  47. {--------------------------------------------------------------------}
  48. { Menu handling procedures }
  49. {--------------------------------------------------------------------}
  50.  
  51. { Handle selection of "About Hello..." item from Apple menu }
  52.  
  53.     procedure DoAppleMenu (item: Integer);
  54.         var
  55.             ignore: Integer;
  56.     begin
  57.         ignore := SkelAlert(aboutAlrtRes, SkelDlogFilter(nil, true), skelPositionOnParentDevice);
  58.         SkelRmveDlogFilter;
  59.     end;
  60.  
  61.  
  62. { Process selection from File menu }
  63.  
  64.     procedure DoFileMenu (item: Integer);
  65.     begin
  66.         case item of
  67.             quit: 
  68.                 SkelStopEventLoop;
  69.         end;
  70.     end;
  71.  
  72.  
  73. { Initialize menus.  Tell TransSkel to process the Apple menu }
  74. { automatically, and associate the proper procedure with the }
  75. { File menu. }
  76.  
  77.     procedure SetupMenus;
  78.         var
  79.             ignore: Boolean;
  80.     begin
  81.         SkelApple('About Hello…', @DoAppleMenu);
  82.         fileMenu := NewMenu(fileMenuNum, 'File');
  83.         AppendMenu(fileMenu, 'Quit/Q');
  84.         ignore := SkelMenu(fileMenu, @DoFileMenu, nil, false, false);
  85.  
  86.         DrawMenuBar;
  87.     end;
  88.  
  89.  
  90. {--------------------------------------------------------------------}
  91. { Window handling procedures }
  92. {--------------------------------------------------------------------}
  93.  
  94. { Draw grow box of window in lower right hand corner }
  95.  
  96.     procedure DrawGrowBox (w: WindowPtr);
  97.         var
  98.             oldClip: RgnHandle;
  99.             r: Rect;
  100.     begin
  101.         r := w^.portRect;
  102.         r.left := r.right - 15;            { draw only in corner }
  103.         r.top := r.bottom - 15;
  104.         oldClip := NewRgn;
  105.         GetClip(oldClip);
  106.         ClipRect(r);
  107.         DrawGrowIcon(w);
  108.         SetClip(oldClip);
  109.         DisposeRgn(oldClip);
  110.     end;
  111.  
  112.  
  113.     procedure Update (resized: Boolean);
  114.         var
  115.             w: WindowPtr;
  116.             r: Rect;
  117.             h, v: Integer;
  118.     begin
  119.         GetPort(w);
  120.         r := w^.portRect;
  121.         EraseRect(r);
  122.         v := (r.top + r.bottom) div 2;
  123.         h := (r.left + r.right - StringWidth(phrase)) div 2;
  124.         MoveTo(h, v);
  125.         DrawString(phrase);
  126.         DrawGrowBox(w);
  127.     end;
  128.  
  129.  
  130. { Normally the grow icon is drawn on a change of activation state, but since }
  131. { the entire portRect is invalidated here, everything will be redrawn by }
  132. { Update(), anyway, including the grow box.  So don't bother here. }
  133.  
  134.     procedure Activate (active: Boolean);
  135.         var
  136.             w: WindowPtr;
  137.     begin
  138.         GetPort(w);
  139.         if (active) then
  140.             phrase := hello
  141.         else
  142.             phrase := goodbye;
  143.         InvalRect(w^.portRect);
  144.     end;
  145.  
  146.  
  147.     procedure Clobber;
  148.         var
  149.             w: WindowPtr;
  150.     begin
  151.         GetPort(w);
  152.         DisposeWindow(w);
  153.     end;
  154.  
  155.  
  156. procedure DoSuspendResume (inForeground: Boolean);
  157. begin
  158.     SkelActivate (FrontWindow, inForeground);
  159. end;
  160.  
  161.  
  162. { Create window and install handler for it.  Mouse and key clicks are }
  163. { ignored.  There is no close proc since the window doesn't have a }
  164. { close box.  There is no idle proc since nothing is done while the }
  165. { window is in front (all the things that are done are handled by }
  166. { TransSkel). }
  167.  
  168.     procedure WindInit;
  169.         var
  170.             w: WindowPtr;
  171.             bounds: Rect;
  172.             ignore: Boolean;
  173.     begin
  174.         if (SkelQuery (skelQInForeground) <> 0) then
  175.             phrase := hello
  176.         else
  177.             phrase := goodbye;
  178.  
  179.         SetRect(bounds, 0, 0, 200, 100);
  180.         if (SkelQuery(skelQHasColorQD) <> 0) then
  181.             w := NewCWindow(nil, bounds, 'Howdy', false, documentProc + 8, WindowPtr(-1), false, 0)
  182.         else
  183.             w := NewWindow(nil, bounds, 'Howdy', false, documentProc + 8, WindowPtr(-1), false, 0);
  184.         SkelPositionWindow(w, skelPositionOnMainDevice, FixRatio(1, 2), FixRatio(1, 5));
  185.         ignore := SkelWindow(w, nil, nil, @Update, @Activate, nil, @Clobber, nil, false);
  186.         TextFont(0);        { select system font in default size }
  187.         TextSize(0);
  188.         SelectWindow(w);
  189.         ShowWindow(w);
  190.     end;
  191.  
  192.  
  193. begin
  194.     SkelInit(nil);
  195.     SetupMenus;
  196.     WindInit;
  197.     SkelSetSuspendResume (@DoSuspendResume);
  198.     SkelEventLoop;
  199.     SkelCleanup;
  200. end.